﻿(function ($) {
    $.fn.acDatePicker = function (options) {

        var opts = $.extend({}, $.fn.acDatePicker.defaults, options);
        var tabKeyCode = '9';
        var previousDate;

        return this.each(function () {
            $(this).datepicker({
                defaultDate: opts.actualDate,
                minDate: "d",
                dateFormat: "dd/mm/yy",
                beforeShow: function (input, inst) {
                    this.previousDate = input.value;
                    $(this).datepicker("setDate", null);
                }
            });

            $(this).keydown(function (event) {
                if (event.keyCode != tabKeyCode) {
                    event.preventDefault();
                } else {
                    var toDateTime = Date.parse(this.previousDate);
                    $(this).datepicker("setDate", toDateTime);
                }
            });
        });
    }

    $.fn.acDatePicker.defaults = {
        actualDate: new Date()
    }
})(jQuery); 